﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SynoManager.DownloadStation.Ftp
{
    public partial class AddFtpTasksDialog : Form
    {
        private readonly FtpBrowserForm parentForm;
        private readonly FtpDirectoryEntry[] entries;
        public AddFtpTasksDialog()
        {
            InitializeComponent();
        }

        public AddFtpTasksDialog(FtpBrowserForm parentForm, FtpDirectoryEntry[] entries)
            :this()
        {
            this.parentForm = parentForm;
            this.entries = entries;
        }

        private void AddFtpTasksDialog_Load(object sender, EventArgs e)
        {
            backgroundWorker.RunWorkerAsync();
        }

        private List<FtpDirectoryEntry> queue = new List<FtpDirectoryEntry>();

        private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            int index = 0;
            foreach(FtpDirectoryEntry entry in entries)
            {
                PrepareEntry(entry);
                if (backgroundWorker.CancellationPending)
                    return;
            }

            foreach (FtpDirectoryEntry entry in queue)
            {
                decimal percentage = ((decimal)100/queue.Count)*index;
                backgroundWorker.ReportProgress((int)percentage, entry);
                parentForm.Download(entry);
                index++;
                if (backgroundWorker.CancellationPending)
                    return;
            }
            backgroundWorker.ReportProgress(100, "Done");
        }

        private void PrepareEntry(FtpDirectoryEntry entry)
        {
            if (entry.IsDirectory)
            {
                backgroundWorker.ReportProgress(0, entry);
                foreach(FtpDirectoryEntry child in parentForm.ftpClient.GetDirectory(entry.Path))
                {
                    PrepareEntry(child);
                }
            }
            else
            {
                queue.Add(entry);
            }

            if (backgroundWorker.CancellationPending)
                return;
        }

        private void backgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            progressBar.Value = e.ProgressPercentage;
            if (e.UserState is string)
            {
                statusLabel.Text = (string) e.UserState;                
            }
            else
            {
                FtpDirectoryEntry entry = (FtpDirectoryEntry) e.UserState;
                if (entry.IsDirectory)
                    statusLabel.Text = "Expanding " + entry.Directory;
                else
                    statusLabel.Text = "Adding " + entry.Name;
            }
        }

        private void backgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            Close();
        }

        private void cancelButton_Click(object sender, EventArgs e)
        {
            backgroundWorker.CancelAsync();
        }
    }
}
